package com.hutao.chargingpile.spread.controller;


import com.hanhan.common.exception.BusinessException;
import com.hanhan.common.result.ResponseEnum;
import com.hutao.chargingpile.spread.pojo.entity.CpClient;
import com.hutao.chargingpile.spread.pojo.entity.CpClientQuery;
import com.hutao.chargingpile.spread.result.R;
import com.hutao.chargingpile.spread.service.CpClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuTao
 * @since 2023-03-28
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/admin/spread/cpClient")
//@CrossOrigin
public class CpClientController {

    @Resource
    private CpClientService cpClientService;

    @ApiOperation(value = "客户管理列表")
    @PostMapping("/list/{page}/{limit}")
    private R findAll(@PathVariable Integer page, @PathVariable Integer limit,
                      CpClientQuery cpClientQuery){

        String mobile = cpClientQuery.getMobile();
        String name = cpClientQuery.getName();
        //起始页
        // page = page-1;
        Map<String,Object> map = new HashMap<>();
        map.put("page",(page -1) * limit);
        map.put("limit",limit);
        map.put("mobile",mobile);
        map.put("name",name);
        System.out.println(name);
        System.out.println(mobile);
        int count = cpClientService.findCount(map);
        return R.ok().datas("list",cpClientService.findAll(map),count);
    }


    @ApiOperation(value = "根据id删除积分等级", notes="逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable Long id){
        boolean result = cpClientService.removeById(id);
        if(result){
            //return R.setResult(ResponseEnum.UPLOAD_ERROR);
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("Excel批量导入数据字典")
    @PostMapping("/import")
    public R batchImport(
            @ApiParam(value = "Excel文件", required = true)
            @RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            cpClientService.importData(inputStream);
            return R.ok().message("批量导入成功");
        } catch (Exception e) {
            //UPLOAD_ERROR(-103, "文件上传错误"),
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }
    }

       @ApiOperation("添加客户信息")
        @PostMapping("/save")
        public R addClient(@ApiParam(value = "客户信息",required = true)
                            CpClient cpClient){
                  cpClientService.addClient(cpClient);
                  return R.ok().data("cpClient",cpClient).message("添加成功");
       }

       @ApiOperation("回显下拉框数据")
        @PostMapping("/find")
        public R find(){
            List<CpClient> list = cpClientService.find();
         return R.ok().data("list",list).message("获取成功");
        }
      @ApiOperation("回显投资商下拉框")
    @PostMapping("/findInvestor")
    public R findInvestor(){
        return R.ok().data("list",cpClientService.findInvestor());
    }

    @ApiOperation("回显性别下拉框")
    @PostMapping("/findSex")
    public R findSex(){
        return R.ok().data("list",cpClientService.findSex());
    }


}

